import phonebar from 'jssip-emicnet/dist/phonebar'
// import phonebar from '../JsSipWrap/dist/phonebar'
import debug from 'debug'
localStorage.setItem('debug', 'phonebar:*')

let log = debug('phonebar:monitor')

log('正在获取用户信息。。。')
//演示账号
let un = 1001
let pwd = '!QAZ2wsx'
//注意：需要根据实际测试企业填入总机号
let switchnumber = '02566684303'
//注册登录预设所属技能组
let gid = 0

//通话结束原因
let callfailedReason = {
    503: '对方忙碌',
    507: '总机号已停机',
    508: '非工作时间',
    512: '该客户今日被呼叫次数已达上限',
    1000: '禁止拨打无权限坐席',
}

//记录同组坐席通话状态通知
let repcalls = {}

//注册指定服务器推送回传
let eventCallback = {
    register: function (res) {
        if (res.code == 200) {
            log('登录成功，等待坐席的呼叫通知')
            log(phonebar.checkLogin())
            seatStatelog()
        }
    },
    callEvent: function (type, data) {
        log(`callback:callEvent, ${type}`)
        log(data)
        seatStatelog()
        let s = phonebar.monitorStatus
        switch (type) {
            case 'answeredPBXCall':
                if (s.ccNumber == data.c)
                    log('这是监听相关呼叫消息，这里可以直接忽略')
                break
            case 'endPBXCall':
                log('停止监听也会收到endPBXCall消息，这里可以直接忽略')
                break
        }
    },
    kickedOffLine: function (data) {
        log(
            `收到下线消息，账户也会被强制下线，这时候只需要更新相应UI就可以;` +
            `下线原因${JSON.stringify(data)}`
        )
        phonebar.logout()
    },
    statusChanged: (data) => {
        if (data.status == '0') {
            log('离线')
        }
        if (data.status == '1') {
            log('在线')
        }
        if (data.status == '2') {
            log('忙碌')
        }
    },
    //接口组员通话通知，通话开始一次，结束一次
    /**
     *  let data = {
            caller,
            ccNumber,
            callee,
            callTime,
            endTime
        }
     */
    repCallStatus: (data) => {
        let record = repcalls[data.caller]
        if (!record) {
            //没有记录，所以是发起呼叫通知
            if (data.endTime != '' || !data.ccNumber || !data.callTime) {
                log(`坐席呼叫通知数据有误 ${JSON.stringify(data)}`)
                return
            }
            let callTime = new Date(data.callTime * 1000)
            log(
                `${data.caller} 在 ${callTime.toLocaleString()} 呼叫 ${data.callee
                }`
            )
            repcalls[data.caller] = data
            log(`注意，5 秒后开始监控`)
            //班长坐席收到普通坐席通话状态通知，5秒后进入监控演示流程
            setTimeout(demoMonitor, 5000)
        } else {
            //有记录，所以是结束呼叫通知
            if (
                !data.endTime ||
                data.ccNumber != record.ccNumber ||
                data.callTime != record.callTime
            ) {
                log(`坐席呼叫通知数据有误 ${JSON.stringify(data)}`)
            } else {
                let endTime = new Date(data.endTime * 1000)
                log(
                    `${data.caller} 在 ${endTime.toLocaleString()} 结束呼叫 ${data.callee
                    }`
                )
            }
            delete repcalls[data.caller]
        }
        log(`目前本组有 ${Object.keys(repcalls).length} 个呼叫`)
    },
}

//监控演示流程-注册登录入口
let call_monitor = async (err, res) => {
    if (err) {
        log('获取用户信息失败', err)
        return
    }
    log('获取用户信息,包含用户信息和组信息')
    let memberInfo = res
    log('用户信息', memberInfo.userData)
    for (const group of memberInfo.inGroups) {
        log('组信息', group)
        //调用init需要设置,如果在多个组，演示登录时就取最后一个组
        gid = group.id
    }

    let params = {
        un: un,
        switchnumber: switchnumber,
        pwd: pwd,
        gid: gid,
        //webrtc-dev.emicloud.com是演示服务器，实际使用请不要用它
        socketUri: 'wss://webrtc-dev.emicloud.com:9060',
    }
    //注册登录易米呼叫服务器
    let reg = phonebar.init(params, eventCallback)
    if (reg) {
        log('phonebar.init 发起注册，注册结果在回调中通知')
    } else {
        log('phonebar.init 没有发起注册，参数有问题')
    }
}
//日志记录坐席当前状态
function seatStatelog(state) {
    var state = phonebar.checkSeatState()
    switch (state) {
        case 0:
            log('当前坐席 离线')
            break
        case 1:
            log('当前坐席 空闲')
            break
        case 2:
            log('当前坐席 忙碌')
            break
        case 3:
            log('当前坐席 振铃')
            break
        case 4:
            log('当前坐席 通话')
            break
        case 5:
            log('当前坐席 保持')
            break
    }
}

//监控演示流程开始-监听
function demoMonitor() {
    let calles = Object.keys(repcalls)
    if (calles.length == 0) {
        log(`本组目前没有通话`)
        return
    }
    let rep = calles[0]
    let repData = repcalls[rep]
    log(`首先静音监听 ${rep} 目前通话 ${repData.ccNumber}`)
    let monitorStatus = phonebar.checkMonitorState()
    log(`开始监听前状态值是 ${JSON.stringify(monitorStatus)}`)
    phonebar.startCallMonitor(repData.ccNumber, (result) => {
        if (result != 0) {
            log(`监听失败`)
            return
        }
        monitorStatus = phonebar.checkMonitorState()
        log(
            `监听成功-开始监听后状态值是 ${JSON.stringify(
                monitorStatus
            )}, 30秒后结束监听`
        )
        setTimeout(demoStopMonitor, 30000, repData.ccNumber)
    })
}

//结束监听，进入强插演示流程
function demoStopMonitor(ccNumber) {
    phonebar.stopCallMonitor(ccNumber, (result) => {
        if (result != 0) {
            log(`退出监听失败`)
            return
        }
        let monitorStatus = phonebar.checkMonitorState()
        log(
            `退出监听成功-目前监听状态值是 ${JSON.stringify(
                monitorStatus
            )} 10 秒后演示监听转强插流程`
        )
        setTimeout(() => {
            phonebar.log(`已过10秒，重新开始监听。演示强插流程`)
            phonebar.startCallMonitor(ccNumber, (result) => {
                if (result != 0) {
                    log(`监听失败`)
                    return
                }
                monitorStatus = phonebar.checkMonitorState()
                log(
                    `监听成功-开始监听后状态值是 ${JSON.stringify(
                        monitorStatus
                    )}, 10秒后开始强插通话`
                )
                setTimeout(demoJoinMonitor, 10000, ccNumber)
            })
        }, 10000)
    })
}
//监听转强插进入三方通话
function demoJoinMonitor(ccNumber) {
    phonebar.joinFromMonitor(ccNumber, (result) => {
        if (result != 0) {
            log(`加入三方通话失败`)
            return
        }
        let monitorStatus = phonebar.checkMonitorState()
        log(
            `强插成功-三方通话中, 目前的监听状态值是 ${JSON.stringify(
                monitorStatus
            )} 30秒后结束强插，退出通话`
        )    
        setTimeout(demoExitJoin, 30000, ccNumber)               
    })    
}
//结束强插 退出三方通话
function demoExitJoin(ccNumber) {    
    phonebar.exitJoinMonitor(ccNumber, (result) => {
        if (result != 0) {
            log(`退出三方通话失败`)
            return
        }
        let monitorStatus = phonebar.checkMonitorState()
        log(
            `已从三方通话退出, 目前的监听状态值是 ${JSON.stringify(
                monitorStatus
            )} 10秒后签出坐席`
        )   
        setTimeout(() => {
            phonebar.log(`已过20秒，退出登录`)
            phonebar.logout()
        }, 10000)     
    })
}

//完成注册登录
function login() {
    phonebar.getUser2(
        {
            un,
            pwd, //密码需要加引号
            switchnumber,
        },
        call_monitor
    )
}
window.login = login
